package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.pojo.dto.CarAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.CarUpdateInfoDTO;
import cn.tedu.fruitshop.pojo.vo.CarDetailsVO;
import cn.tedu.fruitshop.pojo.vo.CarTotalPriceAndTotalWeight;
import cn.tedu.fruitshop.security.LoginPrincipal;
import cn.tedu.fruitshop.service.impl.CarServiceImpl;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/car")
@Api(tags = "08. 购物车管理模块")
public class CarController {
   @Autowired
   CarServiceImpl carService;

   public CarController() {
      log.debug("创建控制器对象：CarController");
   }

   @ApiOperation("添加购物车")
   @ApiOperationSupport(order = 120)
   @PostMapping("/add-new")
   public JsonResult<Void> addNew(@Valid @RequestBody CarAddNewDTO carAddNewDTO,@AuthenticationPrincipal LoginPrincipal loginPrincipal) {
      carAddNewDTO.setUserId(loginPrincipal.getId());
      log.debug("开始处理【添加购物车】的请求，参数：{}", carAddNewDTO);
      carService.addNew(carAddNewDTO);
      log.debug("添加购物车成功！");
      return JsonResult.ok();
   }

   @ApiOperation("根据id删除购物车")
   @ApiOperationSupport(order = 121)
   @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "Integer")
   @PostMapping("/{id:[0-9]+}/delete")
   public JsonResult<Void> delete(@Range(min = 1, message = "删除购物车失败，尝试删除的购物车的ID无效！")
                                  @PathVariable Integer id) {
      log.debug("开始处理【根据id删除购物车】的请求，参数：{}", id);
      carService.delete(id);
      return JsonResult.ok();
   }
   @ApiOperation("查询购物车列表")
   @ApiOperationSupport(order = 123)
   @GetMapping("")
   public JsonResult<List<CarDetailsVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
      log.debug("开始【获取登录信息】，【当前用户名】：{}",loginPrincipal.getId());
      log.debug("开始查询购物车列表请求");
      List<CarDetailsVO> list = carService.list(loginPrincipal.getId());
      return JsonResult.ok(list);
   }

   @ApiOperation("获取购物车总价和总斤数")
   @ApiOperationSupport(order = 126)
   @PostMapping("/total/Price-weight")
   public JsonResult<CarTotalPriceAndTotalWeight> totalPriceWeight(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
      log.debug("开始【获取登录信息】，【当前用户名】：{}",loginPrincipal.getId());
      log.debug("开始查询购物车列表请求");
      CarTotalPriceAndTotalWeight total = carService.getTotalByUserId(loginPrincipal.getId());
      log.info("购物车价格斤数的总数{}",total);
      return JsonResult.ok(total);
   }

   @ApiOperation("查询购物车商品数量")
   @ApiOperationSupport(order = 124)
   @GetMapping("/getCount")
   public JsonResult<Integer> getCount(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
      log.debug("开始【获取登录信息】，【当前用户名】：{}",loginPrincipal.getId());
      log.debug("开始查询购物车列表请求");
      Integer count = carService.getCount(loginPrincipal.getId());
      return JsonResult.ok(count);
   }

   @ApiOperation("根据id修改购物车详情")
   @ApiOperationSupport(order = 310)
   @ApiImplicitParam(name = "id", value = "购物车id", required = true, dataType = "Integer")
   @PostMapping("/{id:[0-9]+}/updateById")
   public JsonResult<Void>updateById(@PathVariable Integer id, CarUpdateInfoDTO carUpdateInfoDTO){
      log.debug("开始处理【修改购物车详情】的请求，参数ID：{}, 新数据：{}", id, carUpdateInfoDTO);
      carService.updateInfoById(id,carUpdateInfoDTO);
      return JsonResult.ok();
   }

   @ApiOperation("根据修改购物车商品")
   @ApiOperationSupport(order = 310)
   @PostMapping("update")
   public JsonResult<Void> update(@RequestBody CarDetailsVO carDetailsVO){
      log.debug("开始处理【修改购物车详情】的请求，参数ID:{}", carDetailsVO);
      carService.update(carDetailsVO);
      return JsonResult.ok();
   }


}
